package com.dragee.modules.officialWebsite.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dragee.common.R;
import com.dragee.config.exception.BusinessErrorType;
import com.dragee.config.exception.BusinessException;
import com.dragee.constants.CommonConstants;
import com.dragee.modules.officialWebsite.entity.CompanyIntroduceEntity;
import com.dragee.modules.officialWebsite.service.CompanyIntroduceService;
import com.dragee.modules.sys.entity.FilesEntity;
import com.dragee.utils.UploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;



/**
 * 公司简介
 *
 * @author tc
 * @email tc@mail.com
 * @date 2023-05-19 09:49:00
 */
@RestController
@RequestMapping("officialWebsite/companyintroduce")
@Api(tags = "公司简介")
public class CompanyIntroduceController {
    @Autowired
    private CompanyIntroduceService companyIntroduceService;

    private final String fileFlag = "officialWebsite_companyintroduce";
    @Value("${webull.mapUploadPath}")
    private String rootPath;


    @PostMapping("upload")
    @ApiOperation("上传文件")
    public R upload(MultipartFile file) {
        if (file == null) {
            throw new BusinessException(BusinessErrorType.INVALID_PARAMS);
        }
        String path = rootPath + CommonConstants.PIC_DIR + "/" + LocalDateTime.now().format(DateTimeFormatter.BASIC_ISO_DATE);
        String fileId = UploadUtil.uploadOne(file, path, fileFlag, null);
        return R.ok().put("data", fileId);
    }

    /**
     * 信息
     */
    @GetMapping("getCompanyIntroduce")
    @ApiOperation("获取公司简介信息")
    public R getCompanyIntroduce(){
		CompanyIntroduceEntity companyIntroduce =
                companyIntroduceService.getOne(new QueryWrapper<CompanyIntroduceEntity>().orderByDesc("createTime"), false);
        if (companyIntroduce == null) {
            companyIntroduce = new CompanyIntroduceEntity();
        } else {
            FilesEntity files = UploadUtil.getFilesEntity(companyIntroduce.getFileId());
            if (files != null) {
                companyIntroduce.setFileName(files.getFileName());
                companyIntroduce.setFilePath(files.getFilePath());
            }
        }
        return R.ok().put("data", companyIntroduce);
    }

    /**
     * 保存
     */
    @PostMapping("saveOrUpdate")
    @ApiOperation("保存或修改公司简介")
    public R saveOrUpdate(@RequestBody CompanyIntroduceEntity companyIntroduce){
        companyIntroduceService.saveOrUpdate(companyIntroduce);
        return R.ok();
    }


    /**
     * 删除
     */
    @GetMapping("delete-by-id")
    @ApiOperation("公司简介删除")
    public R delete( String id){
		companyIntroduceService.removeById(Long.parseLong(id));
        return R.ok();
    }



}
